package org.arain.baseii.system.resources;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.arain.baseii.exception.UseException;
import org.arain.baseii.utils.AppUtils;
import org.arain.baseii.utils.ArainResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
* <p>Title:SysResourcesCpntroller </p>
* @author Arain
* @date2017年8月29日
 */
@Controller
@RequestMapping("system/resource")
public class SysResourcesController {
	@Autowired
	private SysResourcesService sysResourcesService;
	@RequestMapping("list_01")
	public String list_01() {
		return "system/resource/list_01";
	}
	
	@RequestMapping(value="loadResource.do", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String load_grid(HttpServletRequest request,HttpServletResponse response) {
		return sysResourcesService.load_resources_treegrid(request);
	}
	
	@RequestMapping("add_01")
	public String add_01(Model model,SysResourcesDto resourcesDto) {
		resourcesDto.setEnabled("1");
		model.addAttribute("icons", AppUtils.load_icons());
		model.addAttribute("sysResource", resourcesDto);
		return "system/resource/add_01";
	}
	
	@RequestMapping("edit_01")
	public String edit_01(Model model,SysResourcesDto resourcesDto) {
		SysResourcesDto dto = sysResourcesService.load_01(resourcesDto);
		model.addAttribute("icons", AppUtils.load_icons());
		model.addAttribute("sysResource", dto);
		return "system/resource/add_01";
	}
	
	@RequestMapping(value="load_tree.do", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String load_tree(HttpServletRequest request) {
		return sysResourcesService.load_tree(request);
	}
	
	@RequestMapping(value="save.do")
	@ResponseBody
	public ArainResult save_01(SysResourcesDto resourcesDto) {
		if((StringUtils.isBlank(resourcesDto.getPid()) || "0".equals(resourcesDto.getPid()))&& (StringUtils.isBlank(resourcesDto.getResourcesJson()) || !"[]".equals(resourcesDto.getResourcesJson()) )) {
			throw new UseException("菜单模块不允许添加功能配置");
		}
		return sysResourcesService.save_01(resourcesDto);
	}
	
	@RequestMapping(value="load_child",produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String load_child(SysResourcesDto resourcesDto) {
		return sysResourcesService.load_children_resources(resourcesDto);
	}
}
